//
//  LoginView.m
//  JunZeWristband
//
//  Created by Kendama on 2017/8/16.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "LoginView.h"

@implementation LoginView

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        [self masLayoutSubview];
        
    }
    return self;
}

#pragma mark -- 懒加载
- (UILabel *)titleLabel
{
    if(!_titleLabel)
    {
        UIView *superView = self;
        _titleLabel = [[UILabel alloc]init];
        [superView addSubview:_titleLabel];
        [_titleLabel setFont:fontWithSize(20)];
        [_titleLabel setTextColor:[UIColor whiteColor]];
        _titleLabel.text = @"小天机器人";
    }
    return _titleLabel;
}
- (UILabel *)detailLabel
{
    if(!_detailLabel)
    {
        UIView *superView = self;
        _detailLabel = [[UILabel alloc]init];
        [superView addSubview:_detailLabel];
        [_detailLabel setFont:fontCommonSize];
        [_detailLabel setTextColor:[UIColor lightGrayColor]];
        _detailLabel.text = @"请输入手机号登录/注册";
    }
    return _detailLabel;
}



- (PhoneTextFieldView *)phoneView
{
    if(!_phoneView)
    {
        UIView *superView = self;
        _phoneView = [[PhoneTextFieldView alloc]init];
//        _phoneView.iconImageView.image = [UIImage imageNamed:@"icon_phone2"];
        _phoneView.iconImageView.hidden = YES;
        _phoneView.phoneTextField.placeholder = @"请输入手机号";
        [_phoneView.phoneTextField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        _phoneView.phoneTextField.keyboardType = UIKeyboardTypePhonePad;
        [superView addSubview:_phoneView];
    }
    return _phoneView;
}


- (PasswordTextFieldView *)passwordView
{
    if(!_passwordView)
    {
        UIView *superView = self;
        _passwordView = [[PasswordTextFieldView alloc]init];
//        _passwordView.iconImageView.image = [UIImage imageNamed:@"icon_code"];
        _passwordView.iconImageView.hidden = YES;
        _passwordView.passwordTextField.placeholder = @"请输入密码";
        [_passwordView.passwordTextField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [superView addSubview:_passwordView];
    }
    return _passwordView;
}

- (UIButton *)registerButton
{
    if(!_registerButton)
    {
        UIView *superView = self;
        _registerButton = [[UIButton alloc]init];
        [superView addSubview:_registerButton];
        [_registerButton setTitle:@"立即注册" forState:UIControlStateNormal];
        [_registerButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [_registerButton setTitleColor:[UIColor colorWithHexString:@"979797"] forState:UIControlStateNormal];

    }
    return _registerButton;
}
- (UIButton *)forgetButton
{
    if(!_forgetButton)
    {
        UIView *superView = self;
        _forgetButton = [[UIButton alloc]init];
        [superView addSubview:_forgetButton];
        [_forgetButton setTitle:@"忘记密码?" forState:UIControlStateNormal];
        [_forgetButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [_forgetButton setTitleColor:[UIColor colorWithHexString:@"979797"] forState:UIControlStateNormal];

    }
    return _forgetButton;
}

- (UIButton *)loginButton
{
    if(!_loginButton)
    {
        UIView *superView = self;
        _loginButton = [[UIButton alloc]init];
        [superView addSubview:_loginButton];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginButton setTitle:@"不可登录" forState:UIControlStateDisabled];
        [_loginButton.titleLabel setFont:fontCommonSize];
        [_loginButton setBackgroundColor:rgba(0,159,236,1)];
        _loginButton.layer.cornerRadius = 5;

    }
    return _loginButton;
}


- (UIButton *)qqButton
{
    if(!_qqButton)
    {
        UIView *superView = self;
        _qqButton = [[UIButton alloc]init];
        [superView addSubview:_qqButton];
        [_qqButton setBackgroundImage:[UIImage imageNamed:@"QQ"] forState:UIControlStateNormal];
    }
    return _qqButton;
}
- (UIButton *)wechatButton
{
    if(!_wechatButton)
    {
        UIView *superView = self;
        _wechatButton = [[UIButton alloc]init];
        [superView addSubview:_wechatButton];
        [_wechatButton setBackgroundImage:[UIImage imageNamed:@"weixin"] forState:UIControlStateNormal];
    }
    return _wechatButton;
}
- (UIButton *)weiboButton
{
    if(!_weiboButton)
    {
        UIView *superView = self;
        _weiboButton = [[UIButton alloc]init];
        [superView addSubview:_weiboButton];
        [_weiboButton setBackgroundImage:[UIImage imageNamed:@"weibo"] forState:UIControlStateNormal];
    }
    return _weiboButton;
}

- (UIImageView *)thirdLoginImageView
{
    if(!_thirdLoginImageView)
    {
        UIView *superView = self;
        _thirdLoginImageView = [[UIImageView alloc]init];
        [superView addSubview:_thirdLoginImageView];
        _thirdLoginImageView.image = [UIImage imageNamed:@"third_login"];
    }
    return _thirdLoginImageView;
}



#pragma -- 初始化
- (void)masLayoutSubview
{
    __weak UIView *superView = self;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(289));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(90));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(90));
    }];
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).with.offset(px1920Height(20));
        make.left.mas_equalTo(self.titleLabel);
    }];
    
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.detailLabel.mas_bottom).with.offset(px1920Height(80));
        make.left.right.mas_equalTo(self.titleLabel);
        make.height.mas_equalTo(px1920Height(165));
    }];
    
    [self.passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.phoneView.mas_bottom).with.offset(px1920Height(60));
        make.left.right.mas_equalTo(self.titleLabel);
        make.height.mas_equalTo(px1920Height(165));
    }];
    
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.passwordView.mas_bottom).with.offset(px1920Height(30));
        make.left.mas_equalTo(self.titleLabel);
    }];
    
    [self.forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.passwordView.mas_bottom).with.offset(px1920Height(30));
        make.right.mas_equalTo(self.titleLabel);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.forgetButton.mas_bottom).with.offset(px1920Height(120));
        make.left.right.mas_equalTo(self.titleLabel);
        make.height.mas_equalTo(px1920Height(136));
    }];
    
    [self.thirdLoginImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.loginButton.mas_bottom).with.offset(px1920Height(191));
        make.centerX.mas_equalTo(superView.mas_centerX);
//        make.left.and.right.mas_equalTo(superView);
    }];
    
    CGFloat padding = px1080Width(126);
    NSMutableArray *views = [NSMutableArray array];
    [views addObjectsFromArray:@[self.qqButton,self.wechatButton,self.weiboButton]];
    [views mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:px1080Width(160) leadSpacing:padding tailSpacing:padding];
    [views mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.thirdLoginImageView.mas_bottom).with.offset(px1920Height(70));
        UIView *blueView = (UIView *)views[0];
        make.height.mas_equalTo(blueView.mas_width);
    }];
    
//    RAC(self, loginButton.enabled) = [RACSignal combineLatest:@[RACObserve(self, phoneView.phoneTextField.text), RACObserve(self, passwordView.passwordTextField.text)] reduce:^id(NSString *phone, NSString *password) {
//        BOOL enabled = (phone.length > 11 && password.length > 6 && password.length > 20);
//        return @(enabled);
//    }];
                                                                           
}

@end
